package com.jyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jyh.common.R;
import com.jyh.pojo.User;
import com.jyh.service.UserService;
import com.jyh.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMessage(@RequestBody User user){
        //log.info(user.toString());
        //获取邮箱
        String mail = user.getPhone();
        if(mail!=null){
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            String context="验证码："+code+"，您正在进行身份认证，有效期五分钟";

            //发送验证码
            userService.sendMessage(mail,context);

            //将缓存的验证码存入redis,有效时间5分钟
            redisTemplate.opsForValue().set(mail,code,5, TimeUnit.MINUTES);
            log.info("验证码：{}",code);
            return R.success("success");
        }
        return R.error("验证码发送失败");
    }

    /**
     * 登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session){
        //log.info(map.toString());

        String mail = map.get("phone").toString();
        String code = map.get("code").toString();

        //Object codeSession = session.getAttribute(mail);

        //从redis获取验证码
        Object codeSession = redisTemplate.opsForValue().get(mail);
        log.info("验证码:{}",codeSession.toString());

        if(codeSession!=null && codeSession.equals(code)){
            //验证码一致
            //判断用户是否注册
            mail=mail.substring(0,mail.indexOf("@"));// 只保留qq号
            LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone,mail);
            User user = userService.getOne(lqw);
            if (user==null){
                //未注册
                user=new User();

                user.setPhone(mail);
                user.setStatus(1);
                userService.save(user);
            }
            //将用户id存入session
            session.setAttribute("user",user.getId());

            //登录成功销毁验证码
            //log.info("销毁验证码");
            redisTemplate.delete(mail);

            return R.success(user);
        }

        return R.error("验证码错误");
    }

    /**
     * 退出
     * @param httpSession
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return R.success("success");
    }
}
